import {defineStore} from 'pinia'
import { reqLogin,reqUserInfo,reqLogout } from '@/api/user';
import {UserState} from './types/types'
import { loginForm,loginResponseData,userInfoResponseData } from "../../api/user/type";
import {SET_TOKEN,GET_TOKEN,REMOVE_TOKEN} from '@/utils/token'
import { constantRoute } from '@/router/routes';
let useUserStore = defineStore('User',{
    state:():UserState=>{
        return{
            token:GET_TOKEN(),
            menuRoutes:constantRoute,
            username:'', 
            avatar:""
        }
    },
    actions:{
        async userLogin(data:loginForm){
            let result:loginResponseData = await reqLogin(data)
            if(result.code ==200){
                this.token = (result.data as string)
                SET_TOKEN(result.data as string)
                return 'ok'
            }else{
                return Promise.reject(new Error(result.data))
            }
        },
        async userInfo(){
            let result:userInfoResponseData = await reqUserInfo()
            if(result.code==200){
                this.username = result.data.name
                this.avatar = result.data.avatar
                return 'ok'
            }else{
                return Promise.reject(new Error(result.message))
            }
            
        },
        async userLogout(){
            let result:any = await reqLogout()
            if(result.code==200){
                this.token = ''
                this.username = ''
                this.avatar = ''
                REMOVE_TOKEN()
                return 'ok'
            }else{
                return Promise.reject(new Error(result))
            }
            
        }
    },
    getters:{
    
    }
})

export default useUserStore